//
//  ZAssignMissionVC.m
//  WeClubs
//
//  Created by Zed Link on 17/4/2017.
//  Copyright © 2017 Zed. All rights reserved.
//

#import "ZAssignMissionVC.h"
#import "ZTodoMissionCell.h"
#import "ZTodoFlowLayout.h"
#import "ZNewMissionVC.h"
#import "ZMissionModel.h"

static NSString *AssignMissionCellReuseID = @"ZAssignMissionCellReuseID";

@interface ZAssignMissionVC () <UICollectionViewDelegate, UICollectionViewDataSource>

/** 指派任务列表 */
@property (weak, nonatomic) UICollectionView *missionView;

/** 指派任务数据源 */
@property (strong, nonatomic) NSArray <ZTodoMissionModel *> *missionModels;

//任务数据源
@property(nonatomic)NSMutableArray *missionDataArr;

@end

@implementation ZAssignMissionVC

#pragma mark - View Life Cycle
- (void)viewDidLoad{
    
    [super viewDidLoad];
    
    [self setupNav];
    
    [self setupCollectionInit];
    
    [self loadDataFormNetWork];
    
}

#pragma mark - setupUI
- (void)setupCollectionInit{
    
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    // ========== 待办任务列表 ==========
    ZTodoFlowLayout *layout = [[ZTodoFlowLayout alloc] init];
    
    UICollectionView *missionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
    missionView.delegate = self;
    missionView.dataSource = self;
    
    [missionView setBackgroundColor:GENERAL_VIEW_BG_COLOR];
    [missionView registerNib:[UINib nibWithNibName:@"ZTodoMissionCell" bundle:nil] forCellWithReuseIdentifier:AssignMissionCellReuseID];
    missionView.contentInset = UIEdgeInsetsMake(DYNAMIC_TOP_SPACE, 0, DYNAMIC_TOP_SPACE, 0);
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    self.missionView = missionView;
    [self.view addSubview:missionView];
    [self.missionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(UIEdgeInsetsMake(64, 0, 0, 0));
    }];
}

- (void)setupNav{
    
    
    [self zAddBackItemWithGesture:YES];
    self.navigationItem.title = @"任务管理";
    self.navigationItem.rightBarButtonItem = [self zNavItemWithTitle:@"发起任务" andSelector:@selector(createMissionAction)];
}

#pragma mark - loadData
- (void)loadDataFormNetWork{

    
    [MBProgressHUD zAlertProgress:@"正在加载中"];
    
    [HTTPRequest POST:kGget_my_missionUrl parameter:@{@"sponsor_id":@([ZUserInfo sharedInstance].userId)} success:^(id resposeObject) {
        [MBProgressHUD zAlertHideHUD];
    
        if ([resposeObject[@"result_code"] integerValue] == ZNetManagerStatusSuccess) {
            
            for (NSDictionary *dic in resposeObject[@"data"][@"mission"]) {
                
                ZMissionModel * model = [[ZMissionModel alloc]initWithDictionary:dic];
                
                [self.missionDataArr addObject:model];
            }
            [self.missionView reloadData];
            
        }else{
            [MBProgressHUD zAlertMessage:resposeObject[@"result_msg"] to:self.view afterDelay:1.0];
            
        }
    } failure:^(NSError *error) {
        
        [MBProgressHUD zAlertHideHUD];
    }];



}

#pragma mark - UICollectionViewDataSource & UICollectionViewDelegate

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.missionDataArr.count;

}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    ZTodoMissionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:AssignMissionCellReuseID forIndexPath:indexPath];
    cell.type = ZMissionTypeAssign;
    cell.missionModel = self.missionDataArr[indexPath.row];
    
    return cell;
}

#pragma mark - SEL
- (void)createMissionAction
{
    ZNewMissionVC *vc = [ZNewMissionVC zNew];
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark - Setter && Getter Methods
- (NSMutableArray *)missionDataArr{

    if(!_missionDataArr){
    
        _missionDataArr  =[NSMutableArray array];
    
    }
    return _missionDataArr;
}

@end
